package com.webreport.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class DateUtils {
	public static final String STD_DAY_DATE_FORMAT = "yyyy-MM-dd";
	public static final String STD_DATE_FORMAT = "yyyy-MM-dd HH:mm";

	public static int[] getDate() {
		int[] d = new int[7];
		Calendar c = Calendar.getInstance();

		d[0] = c.get(Calendar.YEAR);
		d[1] = c.get(Calendar.MONTH) + 1;
		d[2] = c.get(Calendar.DAY_OF_MONTH);
		d[3] = c.get(Calendar.HOUR_OF_DAY);
		d[4] = c.get(Calendar.MINUTE);
		d[5] = c.get(Calendar.SECOND);
		d[6] = c.get(Calendar.MILLISECOND);

		return d;
	}

	/**
	 * Get current date by format
	 * 
	 * @param format
	 * @return
	 */
	public static String getCurrentDate(String format) {
		String d = null;
		
		if(format!=null && !format.trim().equals("")){
			SimpleDateFormat s = new SimpleDateFormat(format);
			if (s != null)
				d = s.format(Calendar.getInstance().getTime());
		}
		
		return d;
	}

	/**
	 * Get date by format
	 * 
	 * @param date:
	 * @param format
	 * @return
	 */
	public static String getDate(Date date, String format){
		String d = null;
		
		if(date!=null && format!=null && !format.trim().equals("")){
			SimpleDateFormat s = new SimpleDateFormat(format);
			if (s != null)
				d = s.format(date);
		}
		
		return d;
	}
	public static String getSqlTimestamp( java.sql.Timestamp date,String format){
		return getDate(new Date(date.getTime()),format);
	}
	public static String getSqlTimestamp( java.sql.Timestamp date){
		return getSqlTimestamp(date,STD_DATE_FORMAT);
	}
	public static String getDate(Date date){
		return getDate(date,STD_DATE_FORMAT);
	}
	/**
	 * Get date by format
	 * 
	 * @param date
	 * @param format
	 * @return
	 */
	public static Date getDate(String date, String format){
		Date d = null;
		
		if(date!=null && format!=null && !format.trim().equals("")){
			SimpleDateFormat s = new SimpleDateFormat(format);
			if (s != null){
				try{
					d = s.parse(date);
				}catch(ParseException e){
					e.printStackTrace();
				}
			}
		}
		
		return d;
	}
	public static Date getDate(String date){
		return getDate(date,STD_DATE_FORMAT);
	}
	
	public static java.sql.Timestamp getSqlTimestamp(String date, String format){
		 Date d = getDate(date,format);
		 java.sql.Timestamp t = new  java.sql.Timestamp(d.getTime());
		 return t;
	}
	public static java.sql.Timestamp getSqlTimestamp(String date)
	{
		return getSqlTimestamp(date,STD_DATE_FORMAT);
	}
}
